package com.gears_software.sxdc.dccx.ui.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.MyTripListAdapter;
import com.gears_software.sxdc.dccx.data.finder.IFeedBackFinder;
import com.gears_software.sxdc.dccx.data.model.RFeedBackModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterExtras;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/7/26.
 */

@EActivity(R.layout.activity_feedback)
public class FeedBackActivity extends AppCompatActivity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    EditText et_content;
    @ViewById
    Button btn_submit;
    @RestService
    IFeedBackFinder finder;

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

    }

    void initIconFont() {
        actionbar_title.setText("留言反馈");
        back_icon.setVisibility(View.VISIBLE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
    }

    @Click
    void btn_submit() {
        if (et_content.getText().toString().equals("")) {
            TastyToast.makeText(this, "请输入您的宝贵意见", app.SHORT, app.CONFUSING);
        } else {
            RFeedBackModel model = new RFeedBackModel();
            model.setMessageinfo(et_content.getText().toString().trim());
            model.setUsertype("2");
            feedBack(model);
        }

    }

    @Click
    void back_icon() {
        this.finish();
    }

    @Background
    void feedBack(RFeedBackModel model) {
        ResultModel<String> res = finder.feedBack(model);
        showResoult(res);
    }

    @UiThread
    void showResoult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                TastyToast.makeText(this, "添加留言成功", app.SHORT, app.SUCCESS);
                finish();
            } else {
                TastyToast.makeText(this, "添加留言失败", app.SHORT, app.ERROR);
            }
        }
    }


}
